import plugin from '../../../lib/plugins/plugin.js'
import lodash from 'lodash'
import fetch from 'node-fetch'


export class ncp extends plugin {
  constructor () {
    super({
      /** 功能名称 */
      name: 'Yuapi本地版B站N查2.0',
      /** 功能描述 */
      dsc: '简单开发示例',
      /** https://oicqjs.github.io/oicq/#events */
      event: 'message',
      /** 优先级，数字越小等级越高 */
      priority: 1,
      rule: [
        {
          /** 命令正则匹配 */
          reg: '^查公会(.*)$',
          /** 执行方法 */
          fnc: 'cgh'
        },
        {
          /** 命令正则匹配 */
          reg: '^查注销(.*)$',
          /** 执行方法 */
          fnc: 'czx'
        }
      ]
    })
  }


  async cgh (e) {
    /** e.msg 用户的命令消息 */
    logger.info('[用户命令]', e.msg)
    let msg = e.msg.replace("查公会","").trim()
    msg = msg.split(" ")
    if(msg['length']==1){

        let url = `http://127.0.0.1:5187/api/Bili/BiliGuild?sign=${msg[0]}`
       /** 调用接口获取数据 */

    /** 最后回复消息 */
    await e.reply(segment.image(url))
    }
  }

  async czx (e) {
    /** e.msg 用户的命令消息 */
    logger.info('[用户命令]', e.msg)
    let msg = e.msg.replace("查注销","").trim()
    msg = msg.split(" ")
    if(msg['length']==1){
        let url = `http://127.0.0.1:5187/api/Bili/BiliUserOff?sign=${msg[0]}`
        
        try {
          /** 模拟浏览器正常请求 */
          const response = await fetch(url, {
            method: 'GET',
            headers: {
              'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
              'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
              'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
              'Accept-Encoding': 'gzip, deflate',
              'Connection': 'keep-alive',
              'Upgrade-Insecure-Requests': '1'
            }
          })

          if (response.ok) {
            /** 调用接口获取数据 */
            /** 最后回复消息 */
            await e.reply(segment.image(url))
          } else {
            await e.reply(`请求失败，状态码: ${response.status}`)
          }
        } catch (error) {
          logger.error('[查注销] 请求出错:', error)
          await e.reply('请求出错，请稍后再试')
        }
    }
  }
}

  